package test;

import java.util.Scanner;

public class Engine {
	Scanner keyboard = new Scanner(System.in);
	int diceSum;
	boolean sixesThrown = false;
	
	public boolean rollDice(OoTest test, int player) {
		keyboard.nextLine();
		diceSum = test.getSum();
		
		System.out.println(diceSum + " thrown!");
		test.addPoints(player, diceSum);
		
		if (diceSum == 2) {
			this.rolledOnes(test, player);
		}
		System.out.println("Player "+ player +" now has a score of " + test.getScore(player) + "!\n");
		
		if (test.getEqual()) {
			return this.rolledEquals(test, player);
		}
		return false;
	}
	
	public boolean rolledEquals(OoTest test, int player) {
		while(test.getEqual() && test.getScore(player) < 40) {
			System.out.println("Equal pair of dice thrown! Press enter to throw again");
			keyboard.nextLine();

			diceSum = test.getSum();
			System.out.println(diceSum + " thrown!");
			test.addPoints(player, diceSum);
			
			if (diceSum == 2) {
				this.rolledOnes(test, player);
			}
			
			if (test.getSixes() && sixesThrown) {
				System.out.println("player" + player + " threw two sixes two times in a row, and won the game!");
				keyboard.close();
				return true;
			}
			sixesThrown = test.getSixes();
			System.out.println("Player "+ player +" now has a score of " + test.getScore(player) + "!\n");
		}
		sixesThrown = false;
		if (test.getScore(player) >= 40) {
			return this.rollWin(test, player);
		}
		else {
			return false;
		}
	}
	
	public boolean rollWin(OoTest test, int player) {
		System.out.println("Roll two equal dice to win!");
		keyboard.nextLine();
		diceSum = test.getSum();
		
		if (diceSum == 2) {
			this.rolledOnes(test, player);
			return this.rolledEquals(test, player);
		}
		
		if (test.getEqual()) {
			System.out.println("Player "+ player + " won!");
			keyboard.close();
			return true;
		}
		else {
			System.out.println("Dice not equal!");
		}
		return false;
	}
	
	public void rolledOnes(OoTest test, int player) {
		System.out.println("Two ones thrown, score reset!");
		test.setZero(player);
	}
}